<?
	$date_on_cal = 42;

	if($date == ""){
		$year = date("Y");
		$month = date("m");
	}else{
		$year = date("Y", strtotime($date));
		$month = date("m", strtotime($date));
	}

	$start = mktime(0, 0, 0, $month, 1, $year);
	$end = mktime(0, 0, 0, $month+1, 0, $year);
	$time = ($start - (date("N",$start)-1)*DAY);

	$all_event = $dsfm_calendar->get_all_event();
	$all_event = $dsfm_localization->data($all_event, array("topic","detail"));

	$show_event = array();

	$next = date("Y-m-d" , mktime(0, 0, 0, $month+1, 1, $year) );;
	$prev = date("Y-m-d" , mktime(0, 0, 0, $month-1, 1, $year) );;

	$tpl->assign("next", $next);
	$tpl->assign("prev", $prev);
	$tpl->assign("today", date("Y-m-d"));
	$tpl->assign("month", date("F Y",$start));

	$all_date = array();
	for($t = $start; $t <= $end; $t += DAY){
		$all_date[date("Y-m-d",$t)] = array();
	}

	foreach($all_event as $k => $v){
		if( strtotime($v['start_date']) <= $end && strtotime($v['stop_date']) >= $start){

			if( strtotime($v['start_date']) <= $start){
				$v['start_date'] = date("Y-m-d",$start);
			}

			for($t = strtotime($v['start_date']); $t <= strtotime($v['stop_date']) && $t <= $end; $t += DAY){

				$tmp = $all_date[date("Y-m-d", $t)];

				if($v['line'] == ""){
					$v['line'] = "line".count($tmp);
				}

				array_push($tmp, $v);

				$all_date[date("Y-m-d", $t)] = $tmp;
			}
		}
	}

	$tpl->assign("all_date", $all_date);

	$all_box = array();

	for($box=0; $box < $date_on_cal; $box++){

		$tmp = array();
		$boxtime = $time+DAY*$box;

		$tmp['d'] = date("d", $boxtime);
		$tmp['date'] = date("Y-m-d", $boxtime);

		if( count($all_date[date("Y-m-d",$boxtime)]) != 0){
			$tmp['have'] = 1;
		}

		if($boxtime < $start || $boxtime > $end){
			$tmp['class'] = "pass";
		}

		array_push($all_box, $tmp);
	}

	$tpl->assign("all_box", $all_box);
?>